package com.dengtacj.puppeteerspider.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public   class DateUtils {


    /**
     * 获取过去或者未来 任意天内的日期数组
     * @param intervals      intervals天内
     * @return              日期数组
     */
    public static ArrayList<String> test(int intervals ) {
        ArrayList<String> pastDaysList = new ArrayList<>();
        ArrayList<String> fetureDaysList = new ArrayList<>();
        for (int i = 0; i <intervals; i++) {
            pastDaysList.add(getPastDate(i,null));
            fetureDaysList.add(getFetureDate(i,null));
        }
        return pastDaysList;
    }

    /**
     * 获取过去第几天的日期
     *
     * @param past
     * @return
     */
    public static String getPastDate(int past,SimpleDateFormat simpleDateFormat) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) - past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result ="";
        if(simpleDateFormat!=null){
            result = simpleDateFormat.format(today);
        }else{
            result = format.format(today);
        }
        System.out.println(result.toString());
        return result;
    }

    /**
     * 获取未来 第 past 天的日期
     * @param past
     * @return
     */
    public static String getFetureDate(int past,SimpleDateFormat simpleDateFormat) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + past);
        Date today = calendar.getTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String result ="";

        if(simpleDateFormat!=null){
            result = simpleDateFormat.format(today);
        }else{
            result = format.format(today);
        }

        System.out.println(result.toString());
        return result;
    }

    public static Integer compare_date(String startDate, String endDate, DateFormat dateFormat){

        try {
            Date dt1 = dateFormat.parse(startDate);
            Date dt2 = dateFormat.parse(endDate);
            if (dt1.getTime() > dt2.getTime()) {
                System.out.println("startDate 在 endDate 后");
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                System.out.println("startDate 在 endDate 前");
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }

}
